<?php

/**
 * 订单管理
 */

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Models\Admin\Orders;
use App\Models\Admin\OrdersDetail;
use App\Models\Admin\Website;
use App\Models\Admin\Company;
use Illuminate\Routing\Route;
use Illuminate\Support\Facades\Auth;


class OrderController extends Controller
{
    // 0=提交初审待审核，1=初审通过待支付，2=初审失败、3=支付成功提交复审资料，4=复审资料提交待复审、5=复审失败、6=验证完成
    private $Order_Status = ['待初审', '初审通过待支付', '初审失败', '支付完成待提交复审', '待复审', '复审失败', '订单完成'];



    public function index(Request $request)
    {

        $object = Orders::paginate(15, ['*'], 'pageNum');

        return view('admin.Order.index', ['orders' => $object, 'order_status' => $this->Order_Status]);

    }

    public function statistics(Request $request)
    {

        return view('admin.Order.statistics');

    }

    /**
     * 审核订单
     * @param Request $request
     * @param         $order_id 订单ID
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function reviewed(Request $request, $order_id)
    {

        $object = Orders::find($order_id);
        //编辑审核
        if ($request->getMethod() == 'POST') {
            //订单类型
            $order_type=$request->input('order_type');
            //订单状态
            $order_status=$request->input('order_status');
            //是否通过
            $order_reviewed=$request->input('reviewed');
            $order_message=$request->input('message');
            //生效日期
            $effective_date=null;
            //如果是官网认证
            if ($order_type==0){

                switch ($order_status){
                    //如果是初审
                    case 0:
                        //同意初审
                        if($order_reviewed==1){
                            $order_statusSave=1;
                        }else{//拒绝复审
                            $order_statusSave=2;
                        }
                        break;
                    case 2:
                        //拒绝初审
                        if($order_reviewed==1){
                            $order_statusSave=1;
                        }else{//拒绝复审
                            $order_statusSave=2;
                        }
                        break;

                    //如果是复审
                    case 4:
                        //同意复审
                        if($order_reviewed==1){
                            $effective_date=time();
                            $order_statusSave=6;
                        }else{//拒绝复审
                            $order_statusSave=5;
                        }
                        break;

                    case 5:
                        //拒绝复审
                        if($order_reviewed==1){
                            $effective_date=time();
                            $order_statusSave=6;
                        }else{//拒绝复审
                            $order_statusSave=5;
                        }
                        break;
                    default:
                        $order_statusSave=$order_status++;
                }

                $object->order_status=$order_statusSave;
                //保存关联站点信息
                $object->website()->update(['status'=>$order_statusSave,'effective_date'=>$effective_date]);

                //保存订单信息
                $object->save();

                //写入订单明细
                $OrdersDetail = new OrdersDetail();
                $OrdersDetail->order_id = $order_id;
                $OrdersDetail->order_status = $order_statusSave;
                $OrdersDetail->admin_id = Auth::id();
                $OrdersDetail->message = $order_message;
                $OrdersDetail->save();


            }else{//企业实名认证

                switch ($order_status){
                    //如果是初审
                    case 0:
                        //同意初审
                        if($order_reviewed==1){
                            $order_statusSave=1;
                        }else{//拒绝复审
                            $order_statusSave=2;
                        }
                        break;
                    case 2:
                        //拒绝初审
                        if($order_reviewed==1){
                            $order_statusSave=1;
                        }else{//拒绝复审
                            $order_statusSave=2;
                        }
                        break;

                    //如果是复审
                    case 4:
                        //同意复审
                        if($order_reviewed==1){
                            $effective_date=time();
                            $order_statusSave=6;
                        }else{//拒绝复审
                            $order_statusSave=5;
                        }
                        break;

                    case 5:
                        //拒绝复审
                        if($order_reviewed==1){
                            $effective_date=time();
                            $order_statusSave=6;
                        }else{//拒绝复审
                            $order_statusSave=5;
                        }
                        break;
                    default:
                        $order_statusSave=$order_status++;
                }

                $object->order_status=$order_statusSave;
                //保存关联企业信息
                $object->company()->update(['status'=>$order_statusSave,'effective_date'=>$effective_date]);

                //保存订单信息
                $object->save();

                //写入订单明细
                $OrdersDetail = new OrdersDetail();
                $OrdersDetail->order_id = $order_id;
                $OrdersDetail->order_status = $order_statusSave;
                $OrdersDetail->admin_id = Auth::id();
                $OrdersDetail->message = $order_message;
                $OrdersDetail->save();
            }
            $request->session()->flash('order_status', '审批处理成功！');
            return redirect($request->route()->getPrefix().'/order');
        } else {



            if ($object->order_type == 0) {
                $object->website;
                return view('admin.Order.reviewed_website', ['order' => $object, 'order_status' => $this->Order_Status]);
            } else {
                $object->company;
                return view('admin.Order.reviewed_company', ['order' => $object, 'order_status' => $this->Order_Status]);
            }
        }

//        dd($object->toArray());


    }

    public function detailed()
    {
        return view('admin.Order.detailed');

    }



}
